package modeles;

import donnees.dao.ProjectionDao;
import donnees.modele.Projection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

public class ModeleTableProjection extends AbstractTableModel {

    private ProjectionDao projection;
    private List<Projection> conteneur;
    private String[] libelles = {"Horaire", "Titre du film", "Salle", "Categorie Jury"};

    public ModeleTableProjection() {
        projection = new ProjectionDao();
        this.conteneur = new ArrayList<>();
        //charger(new Date());
    }

    public void charger(Date date) {
        fireTableRowsDeleted(0, conteneur.size());
        // Signaler à la Jtable que les lignes vont être supprimées pour mise à jour
        conteneur.clear();
        //Vider le conteneur
        List result;
        try {
            result = projection.listerProjections(date);
            for (Iterator it = result.iterator(); it.hasNext();) {
                Projection p = (Projection) it.next();
                conteneur.add(p);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        fireTableDataChanged();
    }

    public void ajoutProjection(Projection p) {
        conteneur.add(p);
        fireTableDataChanged();
    }

    public void supprimerProjection(Projection p) {
        conteneur.remove(p);
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return conteneur.size();
    }

    @Override
    public int getColumnCount() {
        return libelles.length;
    }

    @Override
    public String getColumnName(int column) {
        return libelles[column];
    }

    @Override
    public Object getValueAt(int row, int column) {
        switch (column) {
            case 0:
                return conteneur.get(row).getHeure();
            case 1:
                return conteneur.get(row).getFilm().getTitre();
            case 2:
                return conteneur.get(row).getSalle().getNom();
            case 3:
                return conteneur.get(row).getFilm().getJury().getCategoriejury();
        }
        return null;
    }
}
